package com.example.netty;


import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.http.*;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.codec.string.LineEncoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

import java.util.Random;
import java.util.concurrent.TimeUnit;

public class NettyServer {

    public static Channel channel ;

    public static void main(String[] args) {

        EventLoopGroup bossEventLoopGroup = new NioEventLoopGroup();
        EventLoopGroup childEventLoopGroup = new NioEventLoopGroup();

        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(bossEventLoopGroup, childEventLoopGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer() {
                    @Override
                    protected void initChannel(Channel ch) throws Exception {
                        ch.pipeline()
                            .addLast(new StringDecoder())
                            .addLast(new StringEncoder())
                            .addLast(new SimpleChannelInboundHandler() {
                                @Override
                                public void channelActive(final ChannelHandlerContext ctx) throws Exception {
                                    super.channelActive(ctx);
                                    channel = ctx.channel();
                                }

                                @Override
                                protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
                                    System.out.println("服务端-接收数据:" + msg);
                                    channel.writeAndFlush(msg+"收到\n");
                                }
                            })
                        .addLast(new IdleStateHandler(10, 0,0, TimeUnit.SECONDS))
                        .addLast(new ChannelInboundHandlerAdapter() {
                            @Override
                            public void userEventTriggered(final ChannelHandlerContext ctx, final Object evt) throws Exception {
                                Channel channel = ctx.channel();
                                System.out.println("监听到事件：" + channel + "=" + evt);
                                if (evt instanceof IdleStateEvent) {//超时事件
                                    IdleStateEvent idleEvent = (IdleStateEvent) evt;
                                    if (idleEvent.state() == IdleState.READER_IDLE) {//读
//                                        ctx.channel().close();
                                        System.out.println("___READER_IDLE___");
                                    } else if (idleEvent.state() == IdleState.WRITER_IDLE) {//写
                                        System.out.println("___WRITER_IDLE___");
                                    } else if (idleEvent.state() == IdleState.ALL_IDLE) {//全部
                                        System.out.println("___ALL_IDLE___");
                                    }
                                }
                                super.userEventTriggered(ctx, evt);
                            }
                        });
                    }
                });
        try {
            ChannelFuture channelFuture = serverBootstrap.bind(7772).sync();
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossEventLoopGroup.shutdownGracefully();
            childEventLoopGroup.shutdownGracefully();
        }
        System.out.println("启动完成");

    }


}
